import { fileURLToPath, URL } from 'node:url'
import type { UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export const loadEnvToConfig = (config:any, env:any) => {
  if(!config.server)config.server = {}
  if(env.ENV_SERVER_PORT){
    config.server.port = env.ENV_SERVER_PORT
  }

  return config
}

// https://cn.vitejs.dev/config/
export default {
  plugins: [
    vue()
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  envPrefix: 'CONF_',
  build: {
    target: 'esnext',
    chunkSizeWarningLimit: 1500,
    sourcemap: false,
    rollupOptions: {
      output: {
        chunkFileNames: 'assets/js/[name]-[hash].js',
        entryFileNames: 'assets/js/[name]-[hash].js',
        assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
        manualChunks(id) {
          if (id.includes('node_modules')) {
            return id.toString().match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//)?.groups!.moduleName ?? 'vender'
          }
        },
      },
    },
  },
} as UserConfig
// vite.config.js

